<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Api\Service\Company\Controllers;

use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\Company;
use App\Eloquent\Ygt\Privilege;
use App\Eloquent\Ygt\Department;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;

class ContactsController extends Controller
{
    public function index()
    {
        $userId             = Func::getHeaderValueByName( 'userid', 1 );
        $departUser         = DepartmentUser::getCurrentInfo( $userId );
        $companyId          = $departUser->company_id;
        $privilegeId        = $departUser->privilege_id;
        $versions           = $companyId.$privilegeId;
        $company            = Company::getInfo(['id'=>$companyId]);
        $companyTitle       = $company->title;
//        $where              = ['company_id'=>$companyId];
//        $fields             = 'id,truename,mobile,department_id';
//        $departList         = Department::getList( $where, 'id,title' )->toArray();
//        $departUserList     = DepartmentUser::getList( $where, $fields )->toArray();
        $departCollection   = Privilege::getDepartmentList($privilegeId);
        //$departList         = $departCollection->toArray();
        $departArr          = $departCollection->pluck('title','id')->all();
        $departIdArr        = $departCollection->keyBy('id')->keys()->toArray();
        $where              = ['company_id'=>$companyId,'department_id'=>['in',$departIdArr]];

        $versions.= '-'.implode('_',$departIdArr);

        $departUserList     = DepartmentUser::getList($where)->toArray();
        $pinyinPath    = base_path( '/framework/Extend/Helpers/pinyin/pinyin.php' );
        require_once $pinyinPath;
        //print_r($departUserList);exit;
        $lists              = [];
        foreach( $departUserList as $key=>$val )
        {
            $departmentId  = $val['department_id'];
            $username       = $val['truename'];
            $usernameGbk   = iconv('UTF-8', 'GBK', $username); //这里utf-8转GBK的用意是，转拼音的那个函数需要GBK
            $flow           = get_pinyin_array($usernameGbk);
            $shortSpellArr = explode("_", $flow[0]);
            $shortSpell     = "";
            for ($s = 1; $s < count($shortSpellArr) - 1; $s++) {
                $shortSpell .= strtoupper(substr($shortSpellArr[$s], 0, 1));
            }
            $longspell      = iconv('GBK', 'UTF-8', str_replace("_", "", $flow[0])); //拼音获得后再转回utf-8
            $shortspell     = iconv('GBK', 'UTF-8', $shortSpell);
//            echo $username.'-1<br/>';
//            echo $longspell.'-2<br/>';
//            echo $shortspell.'-3<br/>';
//            echo '<hr/>';
            $title          = isset($departArr[$departmentId]) ? $departArr[$departmentId] : '';
            $arr            = [
                'id'=>$val['id'], 'title'=>$title, 'username'=>$username,
                'email'=>'','avatar'=>'',
                'workCellPhone'=>$val['mobile'], 'personalCellPhone'=>'',
                'workingPhone'=>'', 'homePhone'=>'',
                'shortNum'=>'',
                'pinyin'=>[$longspell], 'firstPinyin'=>[$shortspell]
            ];
            $lists[]        = $arr;
        }
        $result             = [
            'versions'=>$versions,
            'allUser'=>[[$companyTitle=>$lists]]
        ];
        return $result;
    }
}